package com.njtoyo.taxi.admin.rest.controller.data;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.secondary.entity.OrderBankBill;
import com.njtoyo.taxi.admin.rest.presenter.finance.*;
import com.njtoyo.taxi.admin.rest.wrapper.finance.FinanceIncomeBelongStatWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.finance.FinanceReconciliationWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.finance.FinanceStatWrapper;
import com.njtoyo.taxi.admin.service.finance.FinanceService;

import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;

@RequestMapping("/finance")
@Api(value = "财务管理", tags = "财务管理")
@RestController
public class FinanceController {

    @Autowired
    private FinanceService financeService;

    @GetMapping("/stat")
    @ApiOperation(value = "补贴信息统计", notes = "权限：finance_allowance_statistics")
    @PermissionScope(names = {"finance_allowance_statistics"})
    public RestResult<List<FinanceAllowanceStatsPresenter>> statistics(@Valid FinanceStatWrapper wrapper) {
        return financeService.statistics(wrapper);
    }

    @GetMapping("/income-belong/stat")
    @ApiOperation(value = "收入归属统计", notes = "权限：finance_income_belong_statistics")
    @PermissionScope(names = {"finance_income_belong_statistics"})
    public RestResult<List<FinanceIncomeBelongStatsPresenter>> incomeBelongStatistics(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Valid FinanceIncomeBelongStatWrapper wrapper) {
        return financeService.incomeBelongStatistics(adminUser,wrapper);
    }

    @GetMapping("/income-belong/stat/export")
    public void incomeBelongStatisticsExport(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Valid FinanceIncomeBelongStatWrapper wrapper, HttpServletResponse response) {
         financeService.incomeBelongStatisticsExport(adminUser,wrapper,response);
    }


    @GetMapping("/reconciliation/customer-allowance")
    @ApiOperation(value = "阶段性对账：财务对账.乘客补贴-customerAllowance", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinanceCustomerAllowancePresenter> customerAllowance(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.customerAllowance(wrapper);
    }

    @GetMapping("/reconciliation/service-fee")
    @ApiOperation(value = "阶段性对账：财务对账.信息服务费-serviceFee", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinanceServiceFeePresenter> serviceFee(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.serviceFee(wrapper);
    }

    @GetMapping("/reconciliation/driver-reward")
    @ApiOperation(value = "阶段性对账：财务对账.司机奖励-driverReward", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinanceDriverRewardPresenter> driverReward(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.driverReward(wrapper);
    }

    @GetMapping("/reconciliation/driver-funds-in-out")
    @ApiOperation(value = "阶段性对账：财务对账.司机收入与支出-driverFundsInOut", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinanceDriverFundsInOutPresenter> driverFundsInOut(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.driverFundsInOut(wrapper);
    }

    @GetMapping("/reconciliation/driver-undrawn-balance")
    @ApiOperation(value = "阶段性对账：财务对账.司机未提现总额-driverUndrawnBalance", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinanceUndrawnBalancePresenter> driverUndrawnBalance(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.driverUndrawnBalance(wrapper);
    }

    @GetMapping("/reconciliation/payment-platform-fee")
    @ApiOperation(value = "阶段性对账：财务对账.支付平台手续费-paymentPlatformFee", notes = "权限：finance_reconciliation")
    @PermissionScope(names = {"finance_reconciliation"})
    public RestResult<FinancePaymentPlatformFeePresenter> paymentPlatformFee(@Valid FinanceReconciliationWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getStartDate()) || ObjectUtil.isNull(wrapper.getEndDate())) {
            return RestResult.failed("请选择日期");
        }
        return financeService.paymentPlatformFee(wrapper);
    }


    /**
     * 银行对账
     *
     * @param current
     * @param size
     * @param date
     * @return
     */
    @GetMapping("/bank/bill")
    RestResult<IPage<OrderBankBill>> getBankBillPage(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current, @RequestParam(value = "size", required = false, defaultValue = "15") Integer size, @RequestParam(value = "date", required = false) String date, @RequestParam(value = "result", required = false) String result) {
        return financeService.getBankBillPage(current, size, date, result);
    }


    /**
     * 财务对账
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/comparison")
    public RestResult comparison(@RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate) {
        if (StrUtil.isBlank(startDate) || StrUtil.isBlank(endDate)) {
            return RestResult.failed("请选择查询时间");
        }
        String nowDate = DateUtil.formatDateTime(DateUtil.date());
        if (nowDate.contains(startDate) && nowDate.contains(endDate)) {
            return RestResult.failed("当前数据还没生成结束,请查看其他日期的数据");
        }
        return financeService.comparison(startDate, endDate);
    }
    @GetMapping("/income/total")
    @ApiOperation(value = "收入统计")
    @PassToken
    public RestResult<FinanceIncomeTotal> incomeTotal() {
        FinanceIncomeTotal list = financeService.incomeTotal();
        return RestResult.success(list);
    }

    @GetMapping("/income/total-canton")
    @PassToken
    public RestResult<FinanceIncomeTotal> incomeCantonTotal(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser) {
        FinanceIncomeTotal list = financeService.incomeCantonTotal(adminUser);
        return RestResult.success(list);
    }

}
